package com.hmdp.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.hmdp.dto.Result;
import com.hmdp.entity.ShopType;
import com.hmdp.mapper.ShopTypeMapper;
import com.hmdp.service.IShopTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.utils.RedisConstants;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
public class ShopTypeServiceImpl extends ServiceImpl<ShopTypeMapper, ShopType> implements IShopTypeService {


    @Resource
    private StringRedisTemplate stringRedisTemplate;


    /**
     * 商铺列表catch
     * @return
     */
    @Override
    public Result queryList() {

        String key= RedisConstants.CACHE_TYPE_LIST;
        //1.从redis查询商铺缓存
        String typeJson = stringRedisTemplate.opsForValue().get(key);
        //2.判断是否存在redis中
        if (StrUtil.isNotBlank(typeJson)) {
            //3.存在直接返回
            List<ShopType> shopTypeList = JSONUtil.toList(typeJson, ShopType.class);
            return Result.ok(shopTypeList);
        }
        //4.不存在查询数据库
        List<ShopType> sshopTypeList = query().orderByAsc("sort").list();
        //5.看数据库中是否存在数据
        //7.存在将数据写入redis中
        stringRedisTemplate.opsForValue().set(key,JSONUtil.toJsonStr(sshopTypeList));
        //8.返回
        return Result.ok(sshopTypeList);
    }
}
